package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.model.entity.TeamPriceItemEntity;
import com.sgb.kjwl.model.entity.packers.TeamDetailEntity;
import com.sgb.kjwl.model.entity.packers.TeamPersonEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamDeviceEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TeamServiceModel extends BaseViewModel {

    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> successLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> errorLiveData = new MutableLiveData<>();

    public MutableLiveData<List<FindPackersEntity>> ldFindPackers=new MutableLiveData<>();

    public MutableLiveData<List<TeamPriceItemEntity>> ldTeamPrice=new MutableLiveData<>();

    public MutableLiveData<List<SelfTeamDeviceEntity>> ldTeamDevice=new MutableLiveData<>();

    public MutableLiveData<List<ShopTeamInfoEntity>> ldPerformance=new MutableLiveData<>();

    public MutableLiveData<List<TeamPersonEntity>> ldMember=new MutableLiveData<>();

    public TeamServiceModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getNewList(String companyNo) {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTeam(companyNo).enqueue(new Callback<BaseEntity<List<FindPackersEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<FindPackersEntity>>> call, Response<BaseEntity<List<FindPackersEntity>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                       ldFindPackers.postValue(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<FindPackersEntity>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });

    }

    public void getProfessionDevice(String companyNo){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getProfessionDevice(companyNo).enqueue(new Callback<BaseEntity<List<SelfTeamDeviceEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<SelfTeamDeviceEntity>>> call, Response<BaseEntity<List<SelfTeamDeviceEntity>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        ldTeamDevice.postValue(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<SelfTeamDeviceEntity>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getMember(String companyNo){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getMember(companyNo).enqueue(new Callback<BaseEntity<List<TeamPersonEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TeamPersonEntity>>> call, Response<BaseEntity<List<TeamPersonEntity>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        ldMember.postValue(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TeamPersonEntity>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getQuotePrice(String companyNo){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getQuotePrice(companyNo).enqueue(new Callback<BaseEntity<List<TeamPriceItemEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TeamPriceItemEntity>>> call, Response<BaseEntity<List<TeamPriceItemEntity>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        ldTeamPrice.postValue(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TeamPriceItemEntity>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getPerformance(String companyNo){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getPerformance(companyNo).enqueue(new Callback<BaseEntity<List<ShopTeamInfoEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ShopTeamInfoEntity>>> call, Response<BaseEntity<List<ShopTeamInfoEntity>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        ldPerformance.postValue(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ShopTeamInfoEntity>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }
}
